# configure-bind.pm
# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information: http://carrierclass.net/
#
use strict; use warnings;

package CCNProxy::Provision::Application::bind;
use base qw(CCNCore::Provision::Application::bind);

uses_tables qw( domain server proxy_cluster );

=pod

    make_naptr($name)

        Build a SIP/UDP NAPTR for $name

=cut

sub make_naptr($)
{
    my $name = shift;
    return <<"EOT";
; ${name} IN      NAPTR   0 0 "s" "SISP+D2T" "" _sips._tcp.${name}
; ${name} IN      NAPTR   1 0 "s" "SIP+D2T"  "" _sip._tcp.${name}
${name} IN      NAPTR   2 0 "s" "SIP+D2U"  "" _sip._udp.${name}
EOT
}

=pod

    make_sipsrv($name,$port,$target)
    
        Build a SIP/UDP SRV record for $name going to $port/$target

=cut

sub make_sipsrv($$$)
{
    my $name = shift;
    my $port = shift;
    my $target = shift;
    return <<"EOT";
; _sips._tcp.${name}   IN  SRV 50 0 ${port} ${target}.
; _sip._tcp.${name}    IN  SRV 50 0 ${port} ${target}.
_sip._udp.${name}    IN  SRV 50 0 ${port} ${target}.
EOT
}

=pod
    make_sipsrv_primary($name,$port,$target)
    
        Build a SIP/UDP srv primary target
=cut

sub make_sipsrv_primary($$$)
{
    my $name = shift;
    my $port = shift;
    my $target = shift;
    return <<"EOT";
; _sips._tcp.${name}   IN  SRV 10 0 ${port} ${target}.
; _sip._tcp.${name}    IN  SRV 10 0 ${port} ${target}.
_sip._udp.${name}    IN  SRV 10 0 ${port} ${target}.
EOT
}


sub anchor_system()
{
    my $self = shift;
    my $dnsconf = '';

    # ----- SIP proxy records ----

    # Generate SIP SRV records for domains.
    $self->sql_do_for(<<'SQL', $self->p->system_pk,
        SELECT domain.dns_name, server.px_member, server.id, 
        FROM domain, server, proxy_cluster
        WHERE domain.px_cluster = proxy_cluster.id
        AND server.px_member = proxy_cluster.id
        AND server.the_system = proxy_cluster.the_system
        AND proxy_cluster.the_system = ?
SQL
    # Note: server.the_system = proxy_cluster.the_system should always be true.
    sub
    {
        my ($domain,$px_cluster,$server) = @_;

        my $server_dns = $self$self->p->make_server_name($server);

        # Create an SRV for the internal name
        my $cluster_dns = $self->p->make_cluster_name($px_cluster,'px');
        $dnsconf .= make_sipsrv($cluster_dns,5060,$server_dns);

        # Create an SRV for the domain requested by the user.
        # (This assumes proper IN NS records point to us.)
        $dnsconf .= make_sipsrv($domain,5060,$server_dns);
    });

    # ---- Mediaproxy records ----

    # Generate mediaproxy SRV records for all mediaproxy servers.
    # Note: by default proxy_dispatcher will look for the domain in the RURI first.
    #       We only provide the "default" domain for now.
    #       See mediaproxy.pm's $defaultProxy.
    $self->sql_do_for( <<'SQL', $self->p->system_pk,
        SELECT pk, mp_member 
        FROM server
        WHERE system = ? AND mp_member IS NOT NULL
SQL
    sub
    {
        my ($server_pk,$mp_member) = @_;
        my $server_dns  = $self$self->p->make_server_name($server_pk);
        my $cluster_dns = $self->p->make_cluster_name($mp_member,'mp');
        $dnsconf .= <<"EOT";
_mediaproxy._tcp.${cluster_dns}.  IN SRV   50  25 25060 ${server_dns}.
EOT
    });

    return $dnsconf;
}

1;